﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace OTDR_OPT
{
    /// <summary>
    /// SampleMeterEdit.xaml 的交互逻辑
    /// </summary>
    public partial class SampleMeterEdit : Window
    {
        public SampleMeterEdit()
        {
            InitializeComponent();
            InitWindowData();
        }


        //保存数据
        private void SaveButton_Click(object sender, RoutedEventArgs e)
        {
            string ipAddress = IpAddressBox.Text.Trim();
            string portNum = PortNumBox.Text.Trim();
            if(ipAddress =="" || portNum == "")
            {
                return;
            }
            ConnnectManagement connnect = ConnnectManagement.GetInstance();
            AQ2211 aq2211 = new AQ2211();
            aq2211.IpAddress = ipAddress;
            aq2211.PortNum = portNum;
            connnect.SaveConnctConfig("AQ2211", aq2211); //保存连接数据

            #region 保存修正点数据
            Dictionary<string, List<SampleMeterCorrection>> correcntionDic = new Dictionary<string, List<SampleMeterCorrection>>();
            ItemCollection items = CorrectionTab.Items;
            for (int i = 0; i < items.Count; i++)
            {
                TabItem tabItem = (TabItem)items[i];
                string wavelength = tabItem.Header.ToString();
                Grid lastGrid = GetLastGrid(tabItem);
                if (lastGrid != null)
                {
                    List<SampleMeterCorrection> sampleMeterCorrections = GetSampleMeterCorretionList(lastGrid);
                    if (sampleMeterCorrections.Count != 0)
                    {
                        correcntionDic.Add(wavelength, sampleMeterCorrections);
                    }
                }
            }
            CurrentConfigManagement.GetIntance().SaveSampleMeterCorrection(correcntionDic);
            #endregion
            MessageBox.Show("保存成功","保存标准表信息",MessageBoxButton.OK,MessageBoxImage.Information);
        }


        //查询网格下的修正值
        private List<SampleMeterCorrection> GetSampleMeterCorretionList(Grid parentGrid)
        {
            List<SampleMeterCorrection> sampleMeterCorrectionList = new List<SampleMeterCorrection>();
            foreach (object obj in parentGrid.Children) {
                if (obj.GetType().ToString().EndsWith("SampleMeterCorrectionControl"))
                {
                    SampleMeterCorrectionControl sampleMeterCorrectionControl = (SampleMeterCorrectionControl)obj;
                    string correctionPoint = sampleMeterCorrectionControl.CorrectionPoint;
                    string correctionValue = sampleMeterCorrectionControl.CorrectionValue;
                    if (correctionPoint != null && correctionValue != null)
                    {
                        SampleMeterCorrection sample = new SampleMeterCorrection();
                        sample.CorrectPoint = correctionPoint;
                        sample.CorrectValue = correctionValue;
                        sampleMeterCorrectionList.Add(sample);
                    }
                }
            }
            return sampleMeterCorrectionList;
        }


        //获取内层Grid
        private Grid GetLastGrid(TabItem tabItem)
        {
            Grid grid1 = (Grid)tabItem.Content;
            foreach (Object obj in grid1.Children) {
                if (obj.GetType().ToString().EndsWith("ScrollViewer"))
                {
                    Grid grid2 = (Grid)((ScrollViewer)obj).Content;
                    return grid2;
                }
            }
            return null;
        }


        //初始化窗口数据
        private void InitWindowData()
        {
            #region 初始化IP地址、端口号
            Dictionary<string, string> aq2211Dic = ConnnectManagement.GetInstance().GetObjAllParam("AQ2211");
            if (aq2211Dic != null && aq2211Dic.Count != 0)
            {
                string ipAddress = aq2211Dic["IpAddress"];
                string portNum = aq2211Dic["PortNum"];
                App.Current.Dispatcher.Invoke(new Action(()=> {
                    IpAddressBox.Text = ipAddress;
                    PortNumBox.Text = portNum;
                }));
            }
            #endregion

            #region 从配置文件读取各波长的修正值显示在界面上
            Dictionary<string, List<SampleMeterCorrection>> correctionDic = CurrentConfigManagement.GetIntance().GetLocalCorrecntion();
            ItemCollection items = CorrectionTab.Items;
            for (int i = 0; i < items.Count; i++)
            {
                TabItem tabItem = (TabItem)items[i];
                string wavelength = tabItem.Header.ToString();
                //将修正值添加到相应的波长的选项卡内
                if (correctionDic.Keys.Contains(wavelength))
                {
                    Grid lastGrid = GetLastGrid(tabItem);
                    UIElementCollection elementCollection = lastGrid.Children;
                    List<SampleMeterCorrection> correctionList = correctionDic[wavelength];
                    int correctionCount = correctionList.Count;
                    int existsCount = elementCollection.Count;
                    for(int j = correctionCount; j >= existsCount; j--)
                    {
                        elementCollection = lastGrid.Children;
                        SampleMeterCorrectionControl control = (SampleMeterCorrectionControl)elementCollection[elementCollection.Count - 1];
                        SampleMeterCorrection sampleMeterCorrection = correctionList[correctionCount - j];
                        control.CorrectionPoint = sampleMeterCorrection.CorrectPoint;
                        control.CorrectionValue = sampleMeterCorrection.CorrectValue;
                        if (j != existsCount)
                        {
                            control.AddControl();
                        }
                    }

                }
            }
            #endregion
        }
    }
}
